<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage, ElMessageBox} from "element-plus";


const currentPage = ref(1);
const currentSize = ref(10);
const total = ref(0);
const customerList = ref([]);
const searchCustomer = ref({
  username: '',
  mobile: '',
});
const customerDetail = ref({});
const isEdit = ref(false);
const dialogVisible = ref(false);
const dialogVisibleAdd = ref(false);
const customerAdd = ref({});

const loadCustomers = () => {
  searchCustomer.value.pageSize = currentSize.value;
  searchCustomer.value.pageNum = currentPage.value;
  let data = qs.stringify(searchCustomer.value);
  axios.get(BASE_URL + '/v1/customer/list?' + data).then(res => {
    if (res.data.code === 2000) {
      customerList.value = res.data.data.list;
      total.value = res.data.data.total;
    }
  })
}
const loadCustomerDetail = (row) => {
  axios.get(BASE_URL + `/v1/customer/` + row.id + `/detail`).then(res => {
    if (res.data.code === 2000) {
      console.log(res.data.data);
      customerDetail.value = res.data.data
    }
  })
}
const reSetSearch = () => {
  searchCustomer.value = {};
  loadCustomers();
}
const handleSizeChange = (val) => {
  loadCustomers();
}
const handleCurrentChange = (val) => {
  loadCustomers();
}
const handleWatch = (row) => {
  dialogVisible.value = true;
  loadCustomerDetail(row)
}
const handleEdit = (row) => {
  isEdit.value = true;
  dialogVisible.value = true;
  loadCustomerDetail(row)
}
const handleClose = () => {
  dialogVisible.value = false;
  isEdit.value = false;
}
const handleDelete = (row) => {
  let str = row.state === 0?'启用':'禁用';
  ElMessageBox.confirm('此操作将'+str+'该用户, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    axios.get(BASE_URL + `/v1/customer/`+row.id+`/ban` ).then(res => {
      if (res.data.code === 2000) {
        ElMessage.success(''+str+'成功');
        loadCustomers();
      }
    })
  }).catch(()=>{
    ElMessage.warning(''+str+'取消')
  })
}
const handleAdd = () => {
  let data = qs.stringify(customerAdd.value);
  axios.post(BASE_URL + '/v1/customer/add', data).then(res => {
    if (res.data.code === 2000) {
      ElMessage.success('新增成功');
      dialogVisibleAdd.value = false;
      loadCustomers();
    }
  })
}

onMounted(() => {
  loadCustomers();
})
</script>
<template xmlns="http://www.w3.org/1999/html">
  <h1>客户管理</h1>
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用户名">
        <el-input placeholder="请输入内容" v-model="searchCustomer.username" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="手机号" style="width:290px;">
        <el-input placeholder="请输入内容" v-model="searchCustomer.mobile" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="reSetSearch">重置</el-button>
        <el-button type="primary" @click="loadCustomers">查询</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button type="primary" @click="dialogVisibleAdd=true">新增用户</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card style="margin:20px;">
    <el-table :data="customerList">
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="username" label="用户名" width="100"></el-table-column>
      <el-table-column prop="mobile" label="手机号" width="150"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100"></el-table-column>
      <el-table-column prop="isIdentity" label="是否实名" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.isIdentity===1" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" width="250"></el-table-column>
      <el-table-column prop="state" label="状态" width="200" >
        <template #default="scope">
          <el-tag v-if="scope.row.state===1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
          <el-button v-if="scope.row.state===1" type="text" @click="handleDelete(scope.row)">禁用</el-button>
          <el-button v-else type="text" @click="handleDelete(scope.row)">启用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="text" @click="handleWatch(scope.row)">查看</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :page-sizes="[5, 10, 15, 20]"
                   v-model:current-page="currentPage"
                   v-model:page-size="currentSize"
                   v-model:total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"/>
  </el-card>

  <el-dialog
      title="用户详情"
      v-model="dialogVisible"
      width="50%"
      @close="handleClose"
  >

    <el-form :model="customerDetail" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-if="isEdit" v-model="customerDetail.username" disabled></el-input>
        <span v-else>{{ customerDetail.username }}</span>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-if="isEdit" v-model="customerDetail.mobile" disabled></el-input>
        <span v-else>{{ customerDetail.mobile }}</span>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-if="isEdit" v-model="customerDetail.nickname"></el-input>
        <span v-else>{{ customerDetail.nickname }}</span>
      </el-form-item>
      <el-form-item label="性别">
        <el-tag v-if="customerDetail.gender===1" type="success">男</el-tag>
        <el-tag v-else type="danger">女</el-tag>
      </el-form-item>
      <el-form-item label="注册时间">
        <span style="color: #909399">{{ customerDetail.createTime }}</span>
      </el-form-item>
      <el-form-item label="实名认证">
        <el-tag v-if="customerDetail.isIdentity===1" type="success">是</el-tag>
        <el-tag v-else type="danger">否</el-tag>
      </el-form-item>
      <el-form-item label="真实姓名" v-if="customerDetail.isIdentity===1">
        <span>{{ customerDetail.name }}</span>
      </el-form-item>
      <el-form-item label="身份证号" v-if="customerDetail.isIdentity===1">
        <span>{{ customerDetail.personNum }}</span>
      </el-form-item>


    </el-form>

  </el-dialog>

  <el-dialog
      title="新增用户"
      v-model="dialogVisibleAdd"
      width="61.8%"
      @close="handleClose"
  >
    <el-form :model="customerAdd" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="customerAdd.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="customerAdd.mobile"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="customerAdd.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="customerAdd.nickname"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="customerAdd.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="实名认证">
        <el-radio-group v-model="customerAdd.isIdentity">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="真实姓名" v-if="customerAdd.isIdentity===1">
        <el-input v-model="customerAdd.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" v-if="customerAdd.isIdentity===1">
        <el-input v-model="customerAdd.personNum"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>
<style scoped>

</style>